package br.com.gerenteweb.facesConverter;

import java.io.Serializable;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.com.gerenteweb.daos.LojaDao;
import br.com.gerenteweb.loja.Loja;

@FacesConverter(value = "lojaConverter", forClass = Loja.class)
public class LojaConverter implements Converter, Serializable {

	@Override
	public String getAsString(FacesContext facesContext,
			UIComponent uiComponent, Object object) {
		Loja loja = new Loja();
		System.out.println("getAsString " + object);

		String retorno = "";
		if (object != null) {
			loja = (Loja) object;
			retorno = String.valueOf(loja.getId());
		}
		return retorno;
	}

	@Override
	public Loja getAsObject(FacesContext facesContext, UIComponent uiComponent,
			String value) {
		System.out.println("getAsObject: " + value);
		return new LojaDao().getPorID(Long.parseLong(value));
	}

	/*
	 * @Override public Object getAsObject(FacesContext ctx, UIComponent
	 * component, String value) { if (value == null) { return null; }
	 * 
	 * Loja loja = new Loja();
	 * 
	 * LojaDao lojaDao = new LojaDao(); loja =
	 * lojaDao.getPorID(Long.parseLong(value)); return loja; }
	 */

}